<?php
require_once 'common.php';
require_once 'Course.php';
require_once 'ResultData.php';


if (isGet()) {
    echo "不得以GET访问";
    exit;
}


$courseNo = trim($_REQUEST['courseNo']);
$courseName = trim($_REQUEST['courseName']);

$result = new ResultData();
if ($courseName == "") {
    $result->code = 404;
    $result->msg = "name不能为空";
    exit(json_encode($result));
}

$Course = new Course();
$Course->courseName = $courseName;
$Course->courseNo = $courseNo;


$json_string = file_get_contents('data.json');
// 用参数true把JSON字符串强制转成PHP数组
$data = json_decode($json_string);
$Course->id = count($data) + 1;

array_push($data, $Course);
$json_string = json_encode(array_values($data));

file_put_contents('data.json', $json_string);

exit(json_encode($result));
